package com.nuoniu.sibanyun.service.cm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.cm.ContractType;
import com.nuoniu.sibanyun.mapper.cm.ContractTypeMapper;
import com.nuoniu.sibanyun.service.cm.IContractTypeService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  合同服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-20
 */
@Service
public class ContractTypeServiceImpl extends ServiceImpl<ContractTypeMapper, ContractType> implements IContractTypeService {

    @Override
    public List<ContractType> getList() {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        return list(wrapper);
    }

    @Override
    public void saveContractType(ContractType contractType) {
        UserInfo userInfo = UserThreadLocal.get();
        contractType.setCompanyId(userInfo.getCompanyId());
        ContractType typeByName = baseMapper.getByName(contractType);
        if(typeByName != null && (contractType.getId() == null || !contractType.getId().equals(typeByName.getId()))) {
            throw new NuoNiuException("名称已存在");
        }

        ContractType typeByCode = baseMapper.getByCode(contractType);
        if(typeByCode != null && (contractType.getId() == null || !contractType.getId().equals(typeByCode.getId()))) {
            throw new NuoNiuException("编码已存在");
        }

        this.saveOrUpdate(contractType);
    }
}
